package com.jgzx.security.service;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.jgzx.constant.CacheConstants;
import com.jgzx.domain.LoginUser;
import com.jgzx.domain.LoginVO;
import com.jgzx.redis.config.RedisEnum;
import com.jgzx.redis.login.LoginCache;
import com.jgzx.redis.service.RedisService;
import com.jgzx.utils.AppContextUtil;
import com.jgzx.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author jackesy
 * @className: TokenService
 * @description: Token处理
 * @create 2021/2/4 14:11
 **/
@Service
public class TokenService {

    @Autowired
    private RedisService redisService;


    /**
     * 创建令牌(运营管理平台)
     *
     * @param loginUser
     * @return
     */
    public LoginVO createTokenForPlatform(LoginUser loginUser) {
        Long user_id = loginUser.getUser_id();
        String jwt_token = JwtUtil.createToken(user_id);
        String token_key = CacheConstants.LOGIN_TOKEN_PREFIX_KEY + CacheConstants.LOGIN_TOKEN_FLAG_PLATFORM + loginUser.getUser_id();
        String token = CacheConstants.LOGIN_TOKEN_FLAG_PLATFORM + jwt_token;
        loginUser.setToken(token);
        redisService.set(RedisEnum.LOGIN_CACHE, token_key, JSON.toJSONString(loginUser), CacheConstants.TOKEN_EXPIRE_PLATFORM);
        LoginVO loginVO = new LoginVO();
        loginVO.setToken(token);
        return loginVO;
    }

    /**
     * 创建令牌(企业PC端)
     *
     * @param loginUser
     * @return
     */
    public LoginVO createTokenForEnterprisePC(LoginUser loginUser) {
        Long user_id = loginUser.getUser_id();
        String jwt_token = JwtUtil.createToken(user_id);
        String token_key = CacheConstants.LOGIN_TOKEN_PREFIX_KEY + CacheConstants.LOGIN_TOKEN_FLAG_PC_ENTERPRISE + loginUser.getUser_id();
        String token = CacheConstants.LOGIN_TOKEN_FLAG_PC_ENTERPRISE + jwt_token;
        loginUser.setToken(token);
        redisService.set(RedisEnum.LOGIN_CACHE, token_key, JSON.toJSONString(loginUser), CacheConstants.TOKEN_EXPIRE_PC_ENTERPRISE);
        LoginVO loginVO = new LoginVO();
        loginVO.setToken(token);
        return loginVO;
    }

    /**
     * 创建令牌(企业APP端)
     *
     * @param loginUser
     * @return
     */
    public LoginVO createTokenForEnterpriseAPP(LoginUser loginUser) {
        Long user_id = loginUser.getUser_id();
        String jwt_token = JwtUtil.createToken(user_id);
        String token_key = CacheConstants.LOGIN_TOKEN_PREFIX_KEY + CacheConstants.LOGIN_TOKEN_FLAG_APP_ENTERPRISE + loginUser.getUser_id();
        String token = CacheConstants.LOGIN_TOKEN_FLAG_APP_ENTERPRISE + jwt_token;
        loginUser.setToken(token);
        redisService.set(RedisEnum.LOGIN_CACHE, token_key, JSON.toJSONString(loginUser), CacheConstants.TOKEN_EXPIRE_APP_ENTERPRISE);
        LoginVO loginVO = new LoginVO();
        loginVO.setToken(token);
        return loginVO;
    }

    /**
     * 创建令牌(打刻机端)
     *
     * @param loginUser 登录用户信息
     */
    public LoginVO createTokenForEnterpriseLaserMark(LoginUser loginUser) {
        Long user_id = loginUser.getUser_id();
        String jwt_token = JwtUtil.createToken(user_id);
        String token_key = CacheConstants.LOGIN_TOKEN_PREFIX_KEY + CacheConstants.LOGIN_TOKEN_FLAG_LASER_MARK_ENTERPRISE + loginUser.getUser_id();
        String token = CacheConstants.LOGIN_TOKEN_FLAG_LASER_MARK_ENTERPRISE + jwt_token;
        loginUser.setToken(token);
        redisService.set(RedisEnum.LOGIN_CACHE, token_key, JSON.toJSONString(loginUser), CacheConstants.TOKEN_EXPIRE_LASER_MARK_ENTERPRISE);
        LoginVO loginVO = new LoginVO();
        loginVO.setToken(token);
        return loginVO;
    }

    /**
     * 删除token
     **/
    public void delToken() {
        String token = AppContextUtil.getToken();
        if(StrUtil.isNotEmpty(token)){
            String[] strArr = StrUtil.split(token, ":");
            String token_flag = strArr[0];
            String token_jwt = strArr[1];
            Long user_id = JwtUtil.getUserId(token_jwt);
            String token_key = CacheConstants.LOGIN_TOKEN_PREFIX_KEY + token_flag + ":" + user_id;
            LoginUser loginUser = LoginCache.getLoginUser(token_key);
            if (ObjectUtil.isNotNull(loginUser)
                && StrUtil.equals(token, loginUser.getToken())) {
                redisService.del(RedisEnum.LOGIN_CACHE, token_key);
            }
        }
    }
}